package com.rf.HelloSpringBoot.Controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.rf.HelloSpringBoot.Utils.ResultUtil;
import com.rf.HelloSpringBoot.domain.Result;
import com.rf.HelloSpringBoot.domain.Visitor;
import com.rf.HelloSpringBoot.repository.VisitorRepository;
import com.rf.HelloSpringBoot.service.VisitorService;

/**
 * 
 * @author linzhengchun
 *
 * 2017年7月23日 下午7:21:57
 */
@RestController
public class VisitorController {
	
	@Autowired
	private VisitorRepository visitorRepository;
	
	@Autowired
	private VisitorService visitorService;
	
	/**
	 * 查询所有记录列表
	 * @return
	 */
	@GetMapping(value="/visitors")
	public List<Visitor> visitorList () {
		return this.visitorRepository.findAll(); // 实际开发中，不允许写不带条件的列表查询。
	}
	
	/**
	 * 新增一条记录, Param放body.form-data
	 * @return
	 */
	@PostMapping(value = "/visitors")
	public Visitor visitorAdd(@RequestParam("name") String name, @RequestParam("age") Integer age) {
		Visitor visitor1 = new Visitor();
		visitor1.setName(name);
		visitor1.setAge(age);
		
		return this.visitorRepository.save(visitor1);
	}
	
	/**
	 * 新增一条记录, Param放body.form-data
	 * 简洁的传参方法，加过滤条件
	 * 统一异常处理
	 * @return
	 */
	@PostMapping(value = "/visitors2")
	public Result<Visitor> visitorAdd2(@Valid Visitor visitor1, BindingResult bindRes) {
		if (bindRes.hasErrors()) {
//			System.out.println(bindRes.getFieldError().getDefaultMessage());
			return ResultUtil.error(1, bindRes.getFieldError().getDefaultMessage());
		}
		
		visitor1.setName(visitor1.getName());
		visitor1.setAge(visitor1.getAge());
		
		return ResultUtil.success(this.visitorRepository.save(visitor1));
	}
	
	/**
	 * 根据id查找一条记录
	 * @param id
	 * @return
	 */
	@GetMapping(value="/visitors/{id}")
	public Visitor visitorFindOne(@PathVariable("id") Integer id) {
		return this.visitorRepository.findOne(id);
	}
	
	/**
	 * 更新一条记录，param放body.x-www-form-urlencoded
	 * @param id
	 * @param name
	 * @param age
	 * @return
	 */
	@PutMapping(value="/visitors/{id}")
	public Visitor visitorUpdate(@PathVariable("id") Integer id, @RequestParam("name") String name, @RequestParam("age") Integer age) {
		Visitor visitor1 = new Visitor();
		visitor1.setId(id);
		visitor1.setName(name);
		visitor1.setAge(age);
		
		return this.visitorRepository.save(visitor1);
	}
	
	/**
	 * 删除一条记录
	 * @param id
	 */
	@DeleteMapping(value="/visitors/{id}")
	public void visitorDelete(@PathVariable("id") Integer id) { 
		this.visitorRepository.delete(id);
	}
	
	/**
	 * 拓展查询（age）
	 * @param age
	 * @return
	 */
	@GetMapping(value="/visitors/age/{age}")
	public List<Visitor> visitorListByAge(@PathVariable("age") Integer age) {
		return this.visitorRepository.findByAge(age);
	}
	
	@PostMapping(value="/visitors/two")
	public void visitorTwo() {
		this.visitorService.insertTwo();
	}
	
	@GetMapping(value="/visitors/getAge/{id}")
	public void getAge(@PathVariable("id") Integer id) throws Exception{
		this.visitorService.getAge(id);
	}
}
